package com.bins.rabbitmq.config;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {

	@Bean
	public Queue workQueue() {
		return new Queue("work_queue");
	}

	@Bean
	public Queue queueConfirm() {
		return new Queue("confirm_queue");
	}

	@Bean
	public Queue queueReturn() {
		return new Queue("return_queue");
	}

	@Bean
	public TopicExchange exchangeReturn() {
		return new TopicExchange("return_exchange");
	}

	@Bean
	public Binding bindingReturn() {
		return BindingBuilder.bind(queueReturn()).to(exchangeReturn()).with("return.*");
	}
	
	@Bean
	public Queue queueTTL() {
	    Map<String, Object> map = new HashMap<>(1);
	    map.put("x-message-ttl", 10000);
	    return new Queue("queue_ttl", true, false, false, map);
	}
	
	@Bean
	public Queue queueDLX() {
	    Map<String, Object> map = new HashMap<>(2);
	    // 5秒后，消息自动变为死信
	    map.put("x-message-ttl", 5000);
	    map.put("x-dead-letter-exchange", "exchange_receive");
	    map.put("x-dead-letter-routing-key", "receive_key");
	    return new Queue("queue_dlx", true, false, false, map);
	}

	@Bean
	public DirectExchange exchangeDLX() {
	    return new DirectExchange("exchange_dlx");
	}

	@Bean
	public Binding bindingDLX() {
	    return BindingBuilder.bind(queueDLX()).to(exchangeDLX()).with("receive_key");
	}

	@Bean
	public DirectExchange exchangeReceive() {
	    return new DirectExchange("exchange_receive");
	}

	@Bean
	public Queue queueReceive() {
	    return new Queue("queue_receive");
	}
	
	@Bean
	public Binding bindingReceive() {
	    return BindingBuilder.bind(queueReceive()).to(exchangeReceive()).with("receive_key");
	}
}